import Taro, { Component } from '@tarojs/taro'
import { View, Button, Text, ScrollView } from '@tarojs/components'
import { AtCurtain, AtToast } from 'taro-ui'
import Head from "./components/head"
import AdvList from "./components/advlist"
import Hot from "./components/hot"
import Design from "./components/design"
import Hcompany from "./components/company"
import { post } from '../../service/api'
import './index.scss'

class Index extends Component {
  constructor() {
    super(...arguments)
    this.state = {
      opacity: 0,
      msg: "",
      isShowToast: false,
      isOpened: false
    }
  }
  config = {
    navigationBarTitleText: '欢乐家大平台'
  }
  onClose() {
    this.setState({
      isOpened: false
    })
  }
  onScroll(e) {
    if (e.detail.scrollTop <= 210) {
      this.setState({
        opacity: e.detail.scrollTop / 210
      })
    } else {
      this.setState({
        opacity: 1
      })
    }
  }
  pop(msg) {
    this.setState({
      isShowToast: true,
      msg: msg
    }, () => {
      setTimeout(() => {
        this.setState({
          isShowToast: false
        })
      }, 1500)
    })
  }
  async getVip() {
    const { data } = await post('vipCard/receive')
    if (data.code == 0) {
      this.pop('VIP卡领取成功')
    }
    if (data.code == 2) {
      this.pop('当前账户已拥有该类型vip')
    }
    this.setState({
      isOpened: false
    })
  }
  goDownload() {
    Taro.navigateTo({ url: "/pages/downLoad/index" })
  }
  componentDidShow() {
    let pages = getCurrentPages();
    let url = pages[0].__displayReporter.showReferpagepath;
    const name = 'pages/invitationCode/index.html';
    if (url == name) {
      this.setState({
        isOpened: true
      })
    }

  }
  render() {
    const { opacity, isOpened, isShowToast, msg } = this.state;
    return (
      <ScrollView className='h-index' scrollY onScroll={this.onScroll.bind(this)}>
        <Head opacity={opacity} />
        <AdvList />
        <Hot />
        <Design />
        <Hcompany />
        {/* <View className="goDownload" onClick={this.goDownload.bind(this)}>下载欢乐家APP >></View> */}
        <AtToast
          isOpened={isShowToast}
          text={msg}
          duration={2000}
        ></AtToast>
        <AtCurtain isOpened={isOpened} onClose={this.onClose.bind(this)} >
          <Image className="mask-img" onClick={this.getVip.bind(this)} src={require('../../asset/imgs/zzc2.png')} />
        </AtCurtain>
      </ScrollView>
    )
  }
}

export default Index
